package view.editor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;

import view.Displayable;
import view.View;

import controller.editor.AttributeCreationController;
import javax.swing.JComboBox;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import main.model.Attribute;

public class AttributeCreationView extends View<AttributeCreationController> implements Displayable{
	
	private JFrame frame;
	private JTextField textField;
	private DefaultTableModel attributesToAttr;
	private JTable tablaAtributos;
	private static String[] cardinalidades = { "(0,1)", "(1,1)", "(0,n)", "(1,n)", "(n,n)"};
	@SuppressWarnings("rawtypes")
	private JComboBox comboCardinalidad;
	@SuppressWarnings("rawtypes")
	private JComboBox comboTipo;
	private Attribute attr;
	private AttributeCreationController controller;

	public AttributeCreationView(AttributeCreationController c, Attribute a) {
		this.controller = c;
		attributesToAttr = new DefaultTableModel(){

	 		   /**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
	 		   public boolean isCellEditable(int row, int column) {
					if (column == 0){
						return false;
					}
					return true;
	 		   }
	 		};
	 		attributesToAttr.addColumn ("Attributo");
	 		attributesToAttr.addColumn("Edit");
	 		attributesToAttr.addColumn("Delete");
	 		this.attr = a;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initialize() {
		setFrame(new JFrame("Crear atributo"));
		getFrame().setBounds(100, 100, 539, 507);
		getFrame().getContentPane().setLayout(null);
		
		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setBounds(37, 24, 70, 15);
		getFrame().getContentPane().add(lblNombre);
		
		textField = new JTextField();
		textField.setBounds(163, 22, 188, 19);
		getFrame().getContentPane().add(textField);
		textField.setColumns(10);
		setTablaAtributos(new JTable(getAttributesToAttr()));
		JLabel lblTipo = new JLabel("Tipo:");
		lblTipo.setBounds(37, 80, 70, 15);
		frame.getContentPane().add(lblTipo);
		
		comboTipo = new JComboBox();
		comboTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] {"Caracterización", "Derivado por Copia", "Derivado por Calculo" }));
		comboTipo.setBounds(163, 80, 188, 25);
		frame.getContentPane().add(comboTipo);
		
		JLabel lblCardinalidad = new JLabel("Cardinalidad:");
		lblCardinalidad.setBounds(39, 130, 116, 15);
		frame.getContentPane().add(lblCardinalidad);
		
		comboCardinalidad = new JComboBox();
		comboCardinalidad.setModel(new javax.swing.DefaultComboBoxModel(cardinalidades));
		comboCardinalidad.setBounds(163, 125, 188, 24);
		frame.getContentPane().add(comboCardinalidad);
		
		JLabel lblAtributs = new JLabel("Atributos");
		lblAtributs.setBounds(37, 194, 116, 15);
		frame.getContentPane().add(lblAtributs);
		
		JButton btnAgregarAtributo = new JButton("Agregar atributo");
		btnAgregarAtributo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				controller.handleAddAtribute();
			}
		});
		btnAgregarAtributo.setBounds(163, 189, 188, 25);
		frame.getContentPane().add(btnAgregarAtributo);
		
		JScrollPane scrollPane = new JScrollPane(getTablaAtributos());
		scrollPane.setBounds(41, 253, 431, 174);
		frame.getContentPane().add(scrollPane);
		
		JButton btnNewButton = new JButton("Terminar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				controller.handleButtonTerminar();
			}
		});
		btnNewButton.setBounds(201, 436, 117, 25);
		frame.getContentPane().add(btnNewButton);
	}

	public void fillAttrData() {
		if (this.attr != null){
			this.textField.setText(this.attr.getName());
			int i = 0;
			Integer index = null;
			String card = this.attr.getCardinality().toString();
			Boolean found = Boolean.FALSE;
			while (!found && i<cardinalidades.length){
				if (cardinalidades[i].equals(card)){
					index = i;
					found = Boolean.TRUE;
				}
				i++;
			}
			this.comboCardinalidad.setSelectedIndex(index);
			if (this.attr.getType().equals(Attribute.derivadoxcalc)){
				this.comboTipo.setSelectedIndex(2);
			} else if (this.attr.getType().equals(Attribute.derivadoxcopia)){
				this.comboTipo.setSelectedIndex(1);
			} else {
				this.comboTipo.setSelectedIndex(0);
			}
			addAttributesToTable(this.attr.getAttributes());
		}
	}

	private void addAttributesToTable(List<Attribute> attributes) {
		for (Attribute attribute : attributes) {
			controller.addAttributeToComponent(attribute);
		}
	}

	@Override
	public void show() {
		getFrame().setVisible(true);
	}

	@Override
	public void hide() {
		getFrame().setVisible(false);
	}

	@Override
	public AttributeCreationController getController() {
		return this.controller;
	}

	@Override
	public void setController(AttributeCreationController controller) {
		this.controller = controller;
	}

	private JFrame getFrame() {
		return frame;
	}

	private void setFrame(JFrame frame) {
		this.frame = frame;
	}
	
	public String getAttributeName(){
		return this.textField.getText();
	}
	
	public String getSelectedType(){
		return this.comboTipo.getSelectedItem().toString();
	}
	
	public String getSelectedCardinalidad(){
		return this.comboCardinalidad.getSelectedItem().toString();
	}

	public DefaultTableModel getAttributesToAttr() {
		return attributesToAttr;
	}

	public JTable getTablaAtributos() {
		return tablaAtributos;
	}

	public void setTablaAtributos(JTable tablaAtributos) {
		this.tablaAtributos = tablaAtributos;
	}

	public void setAttr(Attribute attr) {
		this.attr = attr;
	}

	public Attribute getAttr() {
		return attr;
	}
}
